package cn.com.headfree.util;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.headfree.depterprela.service.IDeptErpRelaService;
import cn.com.headfree.entity.TDeptEmpRela;

/**
 * 根据erp_no获取部门全路径工具类
 */
@Component
public class GetFullDeptByErpNoUtil {

    private static final Logger LOG = LogManager.getLogger(GetFullDeptByErpNoUtil.class);

    @Autowired
    private  IDeptErpRelaService service;
    
  private static IDeptErpRelaService deptErpRelaService;
    
  @PostConstruct
  public void init() {
	  deptErpRelaService = service;
  }

    /**
     * 根据个人的erp_no获取部门全路径
     */
    public static String getFullDept(String erpNo){
        List<TDeptEmpRela> deptEmpRelas = deptErpRelaService.getFullDeptPathByErpNo(erpNo);
        List<String> deptFullPaths = new ArrayList<>();
        if(deptEmpRelas != null && deptEmpRelas.size() > 0){
            String path = deptEmpRelas.get(0).getPath();
            String [] deptIds = path.split("[|]");
            if(deptIds != null && deptIds.length > 0){
                    for(int i = 1;i<deptIds.length;i++){
                    	//根据deptId获取部门名称
                    	TDeptEmpRela dept=deptErpRelaService.getDeptInfoByDeptId(deptIds[i]);
                    	if(dept!=null) {
                    		deptFullPaths.add(dept.getDeptName());
                    	}
                    }
            }
        }
        if(deptFullPaths != null && deptFullPaths.size() > 0){
            String path = StringUtils.join(deptFullPaths,"-");
            return path;
        }else {
            return null;
        }
    }



}
